package Stack;

import array.Array;
import array.MyArray;

public class ArrayStack<E> implements Stack<E>{

    private Array<E> array;


    public ArrayStack(){
        this(5);
    }

    public ArrayStack(int capacity){
        array = new Array(capacity);
    }

    @Override
    public void push(E e) {
        array.addLast(e);
    }

    @Override
    public E pop() {
        return array.removeLast();
    }

    @Override
    public E peek() {
        return array.getLast();
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("ArrayStack capacity:"+array.getCapacity()+"size:"+array.getSize()+",data:[");
        for (int i = 0; i < array.getSize(); i++) {
            sb.append(array.get(i));
            if(i != array.getSize() -1){
                sb.append(",");
            }
        }
        return sb.append("]").toString();
    }

    public static void main(String[] args) {
        ArrayStack stack = new ArrayStack(5);
        for (int i = 0; i < 5; i++) {
            stack.push(i);
        }
        System.out.println(stack);
        stack.pop();
        System.out.println(stack);
        Object e = stack.peek();
        System.out.println(e);

    }
}
